﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Logic.Personal;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Web.userControls.Personal
{
    public partial class PersonSendList : System.Web.UI.UserControl
    {
        private int PersonAccountId
        {
            get
            {
                MySession session = new MySession();
                if (session.GetPersonalFromSession() != null)
                    return session.GetPersonalFromSession().PersonAccountid;
                else
                    return 0;
            }
        }
        ApplyJobLogic logic = new ApplyJobLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            msgTab.Visible = false;
            if (!IsPostBack)
            {
                BindCount();
                BindData();
            }

        }

        private void BindCount()
        {
            try
            {
                int count = logic.CountSendByPerson(PersonAccountId);
                if (count > 0)
                {
                    pagerButtom.RecordCount = count;
                    pagerButtom.DataBind();
                    pagerButtom.Visible = true;
                }
                else
                {
                    pagerButtom.Visible = false;
                }

            }
            catch (Exception ex)
            {
                this.gridSend.Visible = false;
                ShowError("查询个人求职列表失败！");
            }
        }
        private void BindData()
        {
            try
            {
                int pageSize = pagerButtom.PageSize;
                int pageIdx = pagerButtom.CurrentPageIndex;
                DataTable dt = logic.GetPageSendByPerson(PersonAccountId, pageSize, pageIdx);
                if (dt == null)
                {
                    ShowError("未查询到个人求职列表！");
                }
                else
                {
                    this.gridSend.DataSource = dt;
                    this.gridSend.DataBind();
                }
            }
            catch (Exception ex)
            {
                this.gridSend.Visible = false;
                ShowError("查询个人求职列表失败！");
            }

        }

        protected void gridSend_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int sendId = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower().Equals("delete"))
            {
                try
                {
                    JobSendResume info = new JobSendResume();
                    info.PersonAccountId = PersonAccountId;
                    info.SendId = sendId;
                    logic.DeleteSendByPerson(info);

                    BindCount();
                    BindData();

                }
                catch (Exception ex)
                {
                    this.msgTd.InnerHtml = "<font color='#990000'> 内部异常，删除简历发送记录失败！</font>";
                    this.msgTd.Visible = true;
                }
            }
                       
        }

        protected void gridSend_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowIndex > -1)
                {
                    try 
                    {
                        HyperLink hlJob = (HyperLink)e.Row.FindControl("hlJob");
                        Label lbJobNo = (Label)e.Row.FindControl("lbJobNo");
                        Label lbJobName = (Label)e.Row.FindControl("lbJobName");
                        hlJob.Text = "(" + lbJobNo.Text + ") " + lbJobName.Text;
                        hlJob.NavigateUrl = WebCommon.GetApplicationPath(Request)  + "Item.aspx?categoryid="
                            + ((int)Constants.CategoryType.Hunter).ToString()
                            + "&subjectid=" + ((int)Constants.SubjectType.HunterPositionList).ToString()
                            + "&jobno=" + lbJobNo.Text.Trim();

                        HyperLink hlResume = (HyperLink) e.Row.FindControl("hlResumeName");
                        Label lbPersonId = (Label) e.Row.FindControl("lbPersonId");
                        hlResume.NavigateUrl = WebCommon.GetApplicationPath(Request)
                            + "Common/ResumeBrowse.aspx?categoryid=1&resumeid=" + lbPersonId.Text;
                    }
                    catch(Exception ex)
                    {}
                }
            }   

        }


        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void ShowError(string message)
        {
            this.gridSend.Visible = false;
            this.msgTab.Visible = true;
            this.msgTd.InnerHtml = "<font color='#990000'>" + message + "</font>";
            this.pagerButtom.Visible = false;
        }

        protected void gridSend_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}